import Frame from "../frameNamespace";
import H5Api from "../other/platform/api/h5Api";
import HuaWeiApi from "../other/platform/api/huaWeiApi";
import OppoApi from "../other/platform/api/oppoApi";
import TtApi from "../other/platform/api/ttApi";
import VivoApi from "../other/platform/api/vivoApi";
import WxApi from "../other/platform/api/wxApi";
import { MiniGameApiBase } from "../other/platform/miniGameBase/miniGameApiBase";

export default class PlatformService extends Frame.ServiceBase {
    protected _serviceName: string = "平台服务";

    private static m_PlatformApiClass: typeof MiniGameApiBase | any = null;

    static platformName:string = "ok";

    private m_PlatformApi:MiniGameApiBase = undefined;
    get platformApi(){ return this.m_PlatformApi; }

    constructor() {
        super();

        this._Initialize();
    }

    _Initialize(): void {
        super._Initialize();

        this.platformCheck();

        this.m_PlatformApi = <MiniGameApiBase>new PlatformService.m_PlatformApiClass();

        console.group("加载平台api");
        //验证授权
        this.m_PlatformApi.validAppId();
        //预先加载
        this.m_PlatformApi.ready();
        console.groupEnd();
    }

    platformCheck(){
        if (window.hasOwnProperty('gamebox')) {
            console.warn('4399游戏盒子');
            // PlatformServer.m_MiniGameApiClass = gameBox4399;
            PlatformService.m_PlatformApiClass = H5Api;
            return;
        }
        console.log('平台'+cc.sys.platform)

        //平台
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME_SUB:
            case cc.sys.WECHAT_GAME: {
                PlatformService.m_PlatformApiClass = WxApi;
                // PlatformService.m_PlatformApiClass = H5Api;
                break;
            }
            case cc.sys.BYTEDANCE_GAME: 
            case cc.sys.BYTEDANCE_GAME_SUB: {
                PlatformService.m_PlatformApiClass = TtApi;
                break;
            }
            case cc.sys.VIVO_GAME: {
                PlatformService.m_PlatformApiClass = VivoApi;
                break;
            }
            case cc.sys.OPPO_GAME: {
                PlatformService.m_PlatformApiClass = OppoApi;
                break;
            }
            case cc.sys.HUAWEI_GAME:{
                PlatformService.m_PlatformApiClass = HuaWeiApi;
                break;
            }
            default: {
                if (cc.sys.isNative) {
                    // PlatformServer.m_MiniGameApiClass = NativeApi;
                    PlatformService.m_PlatformApiClass = H5Api;
                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        // PlatformServer.m_MiniGameApiClass = ksApi;
                        PlatformService.m_PlatformApiClass = H5Api;
                    } else {
                        if (window.hasOwnProperty('h5api')) {
                            // PlatformServer.m_MiniGameApiClass = H5_4399;
                            PlatformService.m_PlatformApiClass = H5Api;
                        } else {
                            PlatformService.m_PlatformApiClass = H5Api;
                        }
                    }
                }
                break;
            }
        }
    }

    static getPlatformApiClass(): typeof MiniGameApiBase {
        return PlatformService.m_PlatformApiClass;
    }

}